package com.jinxin.platform.cdfaceacs.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdfaceacs.bean.CdfaceacsAuthTempalte;
import com.jinxin.platform.cdfaceacs.common.SysUserVO;
import com.jinxin.platform.cdfaceacs.mapper.CdfaceacsAuthTempalteMapper;
import com.jinxin.platform.cdfaceacs.rpc.SysUserRpc;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsAuthTemplateService;
import com.jinxin.platform.cdfaceacs.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-05-20
 * @Author: Lingnan
 **/
@Service
public class FaceAcsAuthTemplateServiceImpl implements IFaceAcsAuthTemplateService {
    @Autowired
    private CdfaceacsAuthTempalteMapper authTempalteMapper;
    @Autowired
    private SysUserRpc sysUserRpc;

    @Override
    public List<CdfaceacsAuthTempalte> list(HashMap param) {
        return authTempalteMapper.list(param);
    }

    @Override
    public List<CdfaceacsAuthTempalte> listPage(HashMap param) {
        return authTempalteMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return authTempalteMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("权限模板Id不存在");
        }
        int result = authTempalteMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdfaceacsAuthTempalte record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if(currentUser == null){return JsonResult.error("获取当前用户失败");}
        String id = record.getId();
        int count;
        String msg = "";
        record.setUpdateUserId(currentUser.getUserId());
        record.setUpdateUser(currentUser.getUserAccount());
        record.setUpdateTime(LocalDateTime.now());
        if(StringUtils.isEmpty(id)){
            record.setId(StringUtil.getUuid());
            count = authTempalteMapper.insert(record);
            msg = "新增成功";
        }else {
            count = authTempalteMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

}
